package com.junsi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.ApproveRole;
import com.junsi.bean.Salestage;
import com.junsi.service.SalestageService;

import net.sf.json.JSONArray;


@Controller
@RequestMapping("/salestage")
public class SalestageController {
/**
 *产品类别控制器
 */
	@Autowired
	private SalestageService salestageService;
	Logger logger = Logger.getRootLogger();

	/**
	 * 新增
	 */
	@RequestMapping("/insert")
	public ModelAndView insert(Salestage salestage, HttpServletResponse response) throws Exception {
		   salestageService.insert(salestage);
		  ModelAndView modelAndView=new ModelAndView();  
		    modelAndView.setViewName("redirect:/salestage/select.action");
		      return modelAndView;
	}

	/**
	 * 刪除
	 */
	@RequestMapping("/deletes")
	public ModelAndView deletes(Integer id, HttpServletResponse response) {
		salestageService.delete(id);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:/salestage/select.action");
		return modelAndView;
	}

	/**
	 * 更新
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/updata")
	public ModelAndView updata(Salestage salestage, HttpServletResponse response) throws Exception {
		salestageService.update(salestage);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:/salestage/select.action");
		return modelAndView;
	}

	/**
	 * 查詢
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/select")
	public ModelAndView select(HttpServletResponse response) throws Exception {
		ModelAndView modelAndView=new ModelAndView();
		List<Salestage> list = salestageService.listAll();
		   modelAndView.addObject("list", list);
		      modelAndView.setViewName("forward:/page/xiaoshou.jsp");
		      return modelAndView;
	}
	/**
	 * 查询产品名称
	 */
	@RequestMapping("/selectname")
	public void selectname(HttpServletResponse response) throws Exception {
		  List<Salestage> approveRoles=salestageService.listAll();
		  List<Map<String, String>> array = new ArrayList<Map<String, String>>();
			for (Salestage arrayList : approveRoles) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("approveRoleId", arrayList.getSaleStageId().toString());
				map.put("flowNme", arrayList.getSaleStageName());
				array.add(map);
			}
			JSONArray jsonArray = JSONArray.fromObject(array);
			response.setContentType("text/html; charset=UTF-8");
			try {
				response.getWriter().write(jsonArray.toString());
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
	}
	
}
